import fs from "fs"
import path from "path"
import camelCase from 'camelcase';

const input = process.argv[2] || '.'
const output = process.argv[3]

const prefix = ""

console.log('convert', input, output)

function scan(input) {
    console.log("scan", input)

    let files = fs.readdirSync(input)
    files.forEach(file => {
        const filename = path.join(input, file)
        const stat = fs.statSync(filename)
        const ext = path.extname(file)
        if (stat.isFile() && ext === ".svg") {
            convert(filename)
        }
        if (stat.isDirectory()) {
            scan(filename)
        }
    })
}

scan(input)

function convert(filename) {
    console.log("convert", filename)

    const ext = path.extname(filename)
    const base = path.basename(filename, ext)
    const out = path.join(output || path.dirname(filename), base + '_svg.ts')


    fs.writeFileSync(out, "// !!! Generated by svg2base64.mjs\n")
    fs.appendFileSync(out, `// From ${filename}\n`)

    let variable = prefix + base + "_svg"
    variable = camelCase(variable, {pascalCase: true})

    fs.appendFileSync(out, `export const ${variable}Base64: string = '`)

    const content = fs.readFileSync(filename, "utf-8")
    const img = 'data:image/svg+xml;base64,' + btoa(content)

    fs.appendFileSync(out, img)
    fs.appendFileSync(out, "'\n")
}
